// pages/adress/adres.js

const app = getApp()
//定义三列数组数据，用于存储省、市、县
var multiArray0 = [];
var multiArray1 = [];
var multiArray2 = [];
// var model = require('../../model/model.js')

var show = false;
var item = {};


Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    url: '',
    adress: [],
    areaPicker: '',
    show: false,
    email:'',
    details: '',
    phone: '',
    name: '',
    yb: '',
    state: '',
    adresId: '',
    userId: '',
    item: {
      show: show
    },
    multiArray: [multiArray0, multiArray1, multiArray2], //封装obj 
    multiIndex: [0, 0, 0],    
    location_id: 2, //默认省份id  
    city_id: "52", //默认城市id,

 
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
 
   //绑定选择器滑动事件  
  bindMultiPickerColumnChange: function(e) {    
     let that = this;    
     console.log('修改的列为', e.detail.column, '，值为', e.detail.value);    
      // 改变第一列
    if (e.detail.column==0){
      var region_id = multiArray0[e.detail.value].region_id;
      console.log(region_id)
      // 根据改变的省份id获得城市列表
      wx.request({
        url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=city',
        data: {
          province: region_id
        },
        success: function (res) {

          let dataArray = res.data;
          multiArray1 = dataArray;
          console.log(multiArray1)
         var data =  that.data.multiArray;
          data[1] = multiArray1;
          // 根据城市列表首个id获得地区列表
          wx.request({
            url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=district',
            data:{
              city: dataArray[0].region_id
            },
            success:function(res){
              console.log(res)
              // 赋值给城市数组
              multiArray2 = res.data;
              data[2] = multiArray2;
            var addrress = that.data.adress;
              var index = that.data.multiIndex;
              index[0] = e.detail.value;
              index[1] = 0;
              index[2] = 0;
              that.setData({
                multiArray: data,
                multiIndex: index
              })
              that.loadCountry(multiArray2[0].region_id)
              addrress[0] = data[0][e.detail.value].region_id;
              addrress[1] = data[1][that.data.multiIndex[1]].region_id;
              addrress[2] = data[2][that.data.multiIndex[2]].region_id;
              console.log(addrress)
              that.setData({
                adress : addrress
              })
             
            }
          })
          
        }
      })
    } else if (e.detail.column == 1){
      wx.request({
        url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=district',
        data:{
          city: multiArray1[e.detail.value].region_id
        },
        success:function(res){
          let dataArray = res.data;
          multiArray2 = dataArray;
          console.log(multiArray2)
          var data = that.data.multiArray;
          data[2] = multiArray2;

          var index = that.data.multiIndex;
          index[1] = e.detail.value;

          index[2] = 0;
          that.setData({
            multiArray: data,
            multiIndex: index
          })
          var addrress = that.data.adress;
          addrress[0] = data[0][that.data.multiIndex[0]].region_id;
          addrress[1] = data[1][e.detail.value].region_id;
          addrress[2] = data[2][that.data.multiIndex[2]].region_id;
          console.log(addrress)
         
          that.setData({
            adress: addrress
          })
        }
      })
    } else if (e.detail.column == 2){
      var data = that.data.multiArray;
      var addrress = that.data.adress;
      addrress[0] = data[0][that.data.multiIndex[0]].region_id;
      addrress[1] = data[1][that.data.multiIndex[1]].region_id;
      addrress[2] = data[2][e.detail.value].region_id;
      var index = that.data.multiIndex;
      index[2] = e.detail.value;
      console.log(index)
      console.log(addrress)
      that.setData({
        adress: addrress,
        multiIndex: index
      })
    }
   
  },
  // 获取省份
  loadProivnce:function(){
    let that = this;
    wx.request({
      url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=province',
      success:function(res){
        console.log(res)
        let dataArray = res.data;
        multiArray0 = dataArray;
        //根据默认省，获取默认市
        that.loadCity(that.data.location_id)
      }
    })
  },
  // 获取城市
  loadCity: function (region_id){
    
    let that = this;
    wx.request({
      url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=city',
      data:{
        province: region_id
      },
      success:function(res){
       
        let dataArray = res.data;
        multiArray1 = dataArray;
        console.log(multiArray1)
        //根据默认市获取对应区
        that.loadCountry(that.data.city_id)
      }
    })
  },
  // 获取地区
  loadCountry: function (location_id){
    let that = this;
    wx.request({
      url: 'https://rt.gzziyu.com/mobile/ziyu.php?Action=district',
      data:{
        city: location_id
      },
      success:function(res){
        let dataArray = res.data;
        multiArray2 = dataArray;
        console.log(dataArray)
        let province_id = that.data.location_id; //默认的省份id 
        let city_id = that.data.city_id; //默认的市区id 
        let multiArray0Index = 0,
          multiArray1Index = 0;
        multiArray0.map(function (v, i) {
          //获取省份所在列的位置
          if (v.region_id == province_id) {
            multiArray0Index = i;
          }
        })
        multiArray1.map(function (v, i) {
          //获取市所在列的位置
          if (v.region_id == city_id) {
            multiArray1Index = i;
          }
        })
        //初始化
        var district_id = that.data.district ? that.data.district : multiArray2[0].region_id; 
        var address = [province_id, city_id, district_id];
        
       console.log(address)
        that.setData({
          adress: address,
          multiArray2: dataArray,
          multiArray: [multiArray0, multiArray1, multiArray2], //封装obj
          multiIndex: [multiArray0Index, multiArray1Index, 0],
          country_id: multiArray2[0].region_id
        })
        if (that.data.district) {
          for (var id in dataArray) {
            if (dataArray[id].region_id == that.data.district) {
              // console.log(that.data.district, id)
              that.setData({
                multiIndex: [multiArray0Index, multiArray1Index, id]
              })
            }
          }
        }

      }
    })
  },
 



  changeAd: function () {
    let that = this;
    console.log(that.data.id)
  
    wx.showLoading({
      title: '处理中',
      mask: true
    })


    let reg = /^1[3|4|5|7|8][0-9]{9}$/

    if (!reg.test(that.data.phone)) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.name == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.details == '') {
      wx.showToast({
        title: '地址详情不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }


    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=act_edit_address',
      data: {
        address_id:that.data.id,
        consignee: that.data.name,
        email: that.data.email,
        zipcode: that.data.zipcode,
        address: that.data.details,
        tel: that.data.phone,
        openid: app.globalData.opid,
        province: that.data.adress[0],
        city: that.data.adress[1],
        district: that.data.adress[2]

      },

      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(res)
        console.log(that.data.id)
        
        var pages = getCurrentPages();
        console.log(that.data.adress)
        for (var i = 0; i < pages.length; i++) {
          if (pages[i].route == 'pages/adressList/adressList' ) {
            pages[i].getAddress()
          }
        }
        wx.showToast({
          title: '成功',
          icon: 'success',
          mask: true,
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
        
      }
    })
  },

  newAd: function () {
    let that = this
    console.log(that.data.adress)
    wx.showLoading({
      title: '处理中',
      mask: true
    })

    let reg = /^1[3|4|5|7|8][0-9]{9}$/

    if (!reg.test(that.data.phone)) {
      wx.showToast({
        title: '手机号码错误',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.name == '') {
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (that.data.details == '') {
      wx.showToast({
        title: '地址详情不能为空',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.request({
      url: app.globalData.url + 'ziyu.php?Action=act_edit_address',
      data: {
        address_id:0,
        consignee: that.data.name,
        address: that.data.details,
        email: that.data.email,
        tel: that.data.phone,
        zipcode: that.data.zipcode,
        openid: app.globalData.opid,
        province: that.data.adress[0],
        city: that.data.adress[1],
        district: that.data.adress[2]

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      success: function (res) {
        console.log(that.data.name),
        console.log(that.data.details),
        console.log(that.data.phone),
        console.log(app.globalData.opid),
        console.log(that.data.adress[0]),
        console.log(that.data.adress[1]),
        console.log(that.data.adress[2])
        console.log(res)
        console.log(that.data.adress)
        var pages = getCurrentPages();

        for (var i = 0; i < pages.length; i++) {
          if (pages[i].route == 'pages/adressList/adressList') {
            pages[i].getAddress()
          }
        }
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          mask: true,
          duration: 2000
        })

        setTimeout(function () {
          // wx.navigateBack({
          //   delta: 1,
          // })
          wx.navigateTo({
            url: './../get-place/get-place'
          })
        }, 2000)

      }
    })
  },

  back: function (e) {
    wx.navigateBack({ delta: 1 })
  },




  details(e) {
    this.setData({
      details: e.detail.value
    })

  },
  phone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  email(e) {
    this.setData({
      email: e.detail.value
    })
  },
  name(e) {
    this.setData({
      name: e.detail.value
    })
  },
  zipcode(e) {
    this.setData({
      zipcode: e.detail.value
    })
  },
  yb(e) {
    this.setData({
      yb: e.detail.value
    })
  },
  // translate(e) {
  //   model.animationEvents(this, 0, true, 400);
  // },
  // //隐藏picker-view
  // hiddenFloatView(e) {
  //   model.animationEvents(this, 200, false, 400);
  // },
  // //滑动事件
  // bindChange: function (e) {
  //   model.updateAreaData(this, 1, e);
  //   console.log(e)
  //   item = this.data.item;
  //   this.setData({
  //     // adress: `${item.provinces[item.value[0]].name} - ${item.citys[item.value[1]].name} - ${item.countys[item.value[2]].name}`
  //   });
  // },



  nono: function () {


  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadProivnce()
    let that = this

    this.setData({
      url: app.globalData.url,
      id: options.id
    })

    if (options.id) {
      console.log(options.id)
      wx.request({
        url: app.globalData.url + 'ziyu.php?Action=act_edit_address&flag=display',
        data: {
          id: options.id,
          // user_id: options.userId,
          openid: app.globalData.opid
        },
        
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success: function (res) {
          console.log(res)
          that.setData({
            name: res.data.consignee,
            details: res.data.address,
            phone: res.data.tel,
            email: res.data.email,
            zipcode: res.data.zipcode,
            adress: [res.data.province, res.data.city, res.data.district],
            location_id: res.data.province,
            city_id:res.data.city,
            district: res.data.district
          })
          
          that.loadProivnce(res.data.province);
          

        }
      })
      
      this.setData({
        state: true,
        adresId: options.id,
        userId: options.userId,
      })


      
    } else {
      console.log(123)
      this.setData({
        state: false,
        adress: ["2", "52", "500"]
      })
    }
   
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    var that = this;
    //请求数据
    // model.updateAreaData(that, 0, e);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})